package cn.cycleif.app.controller;

import cn.cycleif.app.domain.dto.QuestionAddDTO;
import cn.cycleif.app.domain.vo.QuestionVO;
import cn.cycleif.app.service.QuestionService;
import cn.cycleif.util.R;
import cn.dev33.satoken.annotation.SaCheckOr;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Nullable;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Date: 25/2/6-18:40
 * @Desc:
 */
@RequestMapping("/api/question")
@Tag(name = "问题接口", description = "提供对问题进行增删改查的接口")
@RestController
public class QuestionController {

  @Autowired
  private QuestionService questionService;

  @GetMapping("/list")
  @Operation(summary = "获取问题列表")
  @SaIgnore
  public R<Object> getList(
      @RequestParam("order") String order,
      @RequestParam("tag") @Nullable String tagName
  ) {
    return questionService.getPage(order, tagName);
  }

  @PostMapping
  @Operation(summary = "新增问题")
  @SaCheckOr(
      role = @SaCheckRole("staff"),
      permission = @SaCheckPermission("quest.add")
  )
  public R<Integer> addQuestion(@RequestBody @Valid QuestionAddDTO question) {
    return questionService.addQuestion(question, false);
  }


  @PostMapping("/answer")
  @Operation(summary = "新增问题及其答案")
  @SaCheckOr(
      role = @SaCheckRole("staff"),
      permission = @SaCheckPermission("quest.add")
  )
  public R<Integer> addQuestionWithAnswer(@RequestBody @Valid QuestionAddDTO question) {
    return questionService.addQuestion(question, true);
  }

  @PutMapping("/{id}")
  @Operation(summary = "修改问题")
  @SaCheckOr(
      role = @SaCheckRole("staff"),
      permission = @SaCheckPermission("quest.edit")
  )
  public R<Boolean> editQuestion(
      @RequestBody @Valid QuestionAddDTO question,
      @PathVariable("id") Integer id
  ) {
    return questionService.editQuestion(id, question);
  }

  @GetMapping("/{id}")
  @Operation(summary = "获取问题详情")
  @SaIgnore
  public R<QuestionVO> getQuestion(@PathVariable("id") Integer id) {
    return questionService.getQuestion(id);
  }

  @DeleteMapping("/{id}")
  @Operation(summary = "删除问题")
  @SaCheckOr(
      role = @SaCheckRole("staff"),
      permission = @SaCheckPermission("quest.del")
  )
  public R<Integer> deleteQuestion(@PathVariable("id") Integer id) {
    return questionService.deleteQuestion(id);
  }
}
